/*
 * SeedPanel.java
 *
 * Created on 11 May 2006, 19:33
 */

package drayson.weboca.gui;

import drayson.weboca.Utils;
import java.awt.Component;
import java.io.FileNotFoundException;
import java.io.IOException;
import javax.swing.JFileChooser;
import javax.swing.JPanel;



/**
 *
 * @author  Andy Roberts
 */
public class ListPanel extends JPanel {
    
    private JFileChooser inChooser;
    private JFileChooser outChooser;
    
    /** Creates new form SeedPanel */
    public ListPanel() {
        JPanel jp = new JPanel();
        initComponents();
        System.out.println("initComponents has been called");
        
    }
    
    public static String getDescription() {
        
        return "Seeds";
    }
    
    protected String validateContents (Component component, Object o) {
                
        
        if (txtSeeds.getText().trim().length() == 0) {
            return "You must enter at least one seed";
        } else {
            return null;
        }
    }
    
    private JFileChooser getInChooser() {
    
        if (inChooser == null) {
            inChooser = new JFileChooser();
        }
        
        return inChooser;
    }
    
    private JFileChooser getOutChooser() {
    
        if (outChooser == null) {
            outChooser = new JFileChooser();
        }
        
        return outChooser;
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
    private void initComponents() {
        btnLoad = new javax.swing.JButton();
        btnSave = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        txtSeeds = new javax.swing.JTextArea();
        jButton1 = new javax.swing.JButton();

        btnLoad.setText("Load custom");
        btnLoad.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnLoadActionPerformed(evt);
            }
        });

        btnSave.setText("Save custom");
        btnSave.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSaveActionPerformed(evt);
            }
        });

        jLabel1.setText("Enter a term per line");

        txtSeeds.setColumns(20);
        txtSeeds.setRows(5);
        txtSeeds.setName("txtSeeds");
        //txtSeeds.setText("arabic\nconcordance\nlanguage\nlearning\ncluster");
        jScrollPane1.setViewportView(txtSeeds);

        jButton1.setText("Load golden list");

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 384, Short.MAX_VALUE)
                    .add(layout.createSequentialGroup()
                        .add(btnLoad)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(btnSave)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 85, Short.MAX_VALUE)
                        .add(jButton1))
                    .add(jLabel1))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .add(jLabel1)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 204, Short.MAX_VALUE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(btnLoad)
                    .add(btnSave)
                    .add(jButton1))
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void btnSaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSaveActionPerformed

        int returnVal = getOutChooser().showOpenDialog(this);
    
        if(returnVal == JFileChooser.APPROVE_OPTION) {
            try {
                Utils.saveFile(txtSeeds.getText(), getOutChooser().getSelectedFile());
                
            } catch (FileNotFoundException ex) {
                ex.printStackTrace();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
    }//GEN-LAST:event_btnSaveActionPerformed

    private void btnLoadActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnLoadActionPerformed

        int returnVal = getInChooser().showOpenDialog(this);
    
        if(returnVal == JFileChooser.APPROVE_OPTION) {
            try {
                txtSeeds.setText(Utils.loadFile(getInChooser().getSelectedFile()).trim());
            } catch (FileNotFoundException ex) {
                ex.printStackTrace();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
    }//GEN-LAST:event_btnLoadActionPerformed
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnLoad;
    private javax.swing.JButton btnSave;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    protected javax.swing.JTextArea txtSeeds;
    // End of variables declaration//GEN-END:variables
    
}
